package com.bawei.rk_01

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.utils.mvvm.BaseAcitivity
import com.bawei.rk_01.Login.LoginViewModel
import com.bawei.rk_01.databinding.ActivityXiuGaiBinding
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import okhttp3.internal.notify

@Route(path = "/app/XiuGaiActivity")

class XiuGaiActivity : BaseAcitivity<ActivityXiuGaiBinding, LoginViewModel>() {
    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_xiu_gai
    }

    override fun initView() {

        val phone = SPUtils.getInstance().getString("phone")
        val replaceRange = phone.replaceRange(3, 7, "*****")
        binding.tvPhone.text = "当前手机号：$replaceRange"

        //发送验证码，发送通知
        binding.tvGetcode.setOnClickListener {
            viewModel.getCode(phone)
        }

        //点击修改密码
        binding.btnCode.setOnClickListener {
            viewModel.xiugai(phone,binding.etPwd1.text.toString(),binding.etPwd.text.toString())
        }

    }

    @SuppressLint("NotificationPermission")
    override fun initData() {

        viewModel.codesuccess.observe(this){
            if (it.code == 200){
                //设置通知

                val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                //版本处理，添加通道 安卓九以上

                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O){
                    //高版本
                    notificationManager.createNotificationChannel(NotificationChannel("aa","aa",NotificationManager.IMPORTANCE_DEFAULT))
                    val intent = Intent(this, XiuGaiActivity::class.java)
                    val activities = PendingIntent.getActivity(
                        this,
                        101,
                        intent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                    )
                    val builder = Notification.Builder(this)
                        .setChannelId("aa")
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentTitle("验证码")
                        .setContentTitle("验证码${it.data.content.substring(8,14)}")
                        .setAutoCancel(true)//点击通知消失
                        .setContentIntent(activities)
                        .build()
                    notificationManager.notify(1,builder)
                }else{
                    //低版本
                    val builder = Notification.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentTitle("验证码")
                        .setContentTitle("验证码${it.data.content.substring(8,14)}")
                        .build()
                    notificationManager.notify(1,builder)
                }
            }
        }

        viewModel.xgsuccess.observe(this){
            if (it.code == 200){
                ToastUtils.showLong("修改成功,请重新登录")
                ARouter.getInstance().build("/app/LoginActivity").navigation()
            }
        }

    }

}